{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 7.8 \u51cf\u5c11\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u53c2\u6570\u4e2a\u6570\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u6709\u4e00\u4e2a\u88ab\u5176\u4ed6python\u4ee3\u7801\u4f7f\u7528\u7684callable\u5bf9\u8c61\uff0c\u53ef\u80fd\u662f\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u6216\u8005\u662f\u4e00\u4e2a\u5904\u7406\u5668\uff0c\n\u4f46\u662f\u5b83\u7684\u53c2\u6570\u592a\u591a\u4e86\uff0c\u5bfc\u81f4\u8c03\u7528\u65f6\u51fa\u9519\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u9700\u8981\u51cf\u5c11\u67d0\u4e2a\u51fd\u6570\u7684\u53c2\u6570\u4e2a\u6570\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 functools.partial() \u3002\npartial() \u51fd\u6570\u5141\u8bb8\u4f60\u7ed9\u4e00\u4e2a\u6216\u591a\u4e2a\u53c2\u6570\u8bbe\u7f6e\u56fa\u5b9a\u7684\u503c\uff0c\u51cf\u5c11\u63a5\u4e0b\u6765\u88ab\u8c03\u7528\u65f6\u7684\u53c2\u6570\u4e2a\u6570\u3002\n\u4e3a\u4e86\u6f14\u793a\u6e05\u695a\uff0c\u5047\u8bbe\u4f60\u6709\u4e0b\u9762\u8fd9\u6837\u7684\u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def spam(a, b, c, d):\n    print(a, b, c, d)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u73b0\u5728\u6211\u4eec\u4f7f\u7528 partial() \u51fd\u6570\u6765\u56fa\u5b9a\u67d0\u4e9b\u53c2\u6570\u503c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from functools import partial\ns1 = partial(spam, 1) # a = 1\ns1(2, 3, 4)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s1(4, 5, 6)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s2 = partial(spam, d=42) # d = 42\ns2(1, 2, 3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s2(4, 5, 5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s3 = partial(spam, 1, 2, d=42) # a = 1, b = 2, d = 42\ns3(3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s3(4)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s3(5)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53ef\u4ee5\u770b\u51fa partial() \u56fa\u5b9a\u67d0\u4e9b\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684callable\u5bf9\u8c61\u3002\u8fd9\u4e2a\u65b0\u7684callable\u63a5\u53d7\u672a\u8d4b\u503c\u7684\u53c2\u6570\uff0c\n\u7136\u540e\u8ddf\u4e4b\u524d\u5df2\u7ecf\u8d4b\u503c\u8fc7\u7684\u53c2\u6570\u5408\u5e76\u8d77\u6765\uff0c\u6700\u540e\u5c06\u6240\u6709\u53c2\u6570\u4f20\u9012\u7ed9\u539f\u59cb\u51fd\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u8981\u89e3\u51b3\u7684\u95ee\u9898\u662f\u8ba9\u539f\u672c\u4e0d\u517c\u5bb9\u7684\u4ee3\u7801\u53ef\u4ee5\u4e00\u8d77\u5de5\u4f5c\u3002\u4e0b\u9762\u6211\u4f1a\u5217\u4e3e\u4e00\u7cfb\u5217\u7684\u4f8b\u5b50\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u662f\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u70b9\u7684\u5217\u8868\u6765\u8868\u793a(x,y)\u5750\u6807\u5143\u7ec4\u3002\n\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u51fd\u6570\u6765\u8ba1\u7b97\u4e24\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "points = [ (1, 2), (3, 4), (5, 6), (7, 8) ]\n\nimport math\ndef distance(p1, p2):\n    x1, y1 = p1\n    x2, y2 = p2\n    return math.hypot(x2 - x1, y2 - y1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u73b0\u5728\u5047\u8bbe\u4f60\u60f3\u4ee5\u67d0\u4e2a\u70b9\u4e3a\u57fa\u70b9\uff0c\u6839\u636e\u70b9\u548c\u57fa\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u6765\u6392\u5e8f\u6240\u6709\u7684\u8fd9\u4e9b\u70b9\u3002\n\u5217\u8868\u7684 sort() \u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u5173\u952e\u5b57\u53c2\u6570\u6765\u81ea\u5b9a\u4e49\u6392\u5e8f\u903b\u8f91\uff0c\n\u4f46\u662f\u5b83\u53ea\u80fd\u63a5\u53d7\u4e00\u4e2a\u5355\u4e2a\u53c2\u6570\u7684\u51fd\u6570(distance()\u5f88\u660e\u663e\u662f\u4e0d\u7b26\u5408\u6761\u4ef6\u7684)\u3002\n\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 partial() \u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "pt = (4, 3)\npoints.sort(key=partial(distance,pt))\npoints"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u66f4\u8fdb\u4e00\u6b65\uff0cpartial() \u901a\u5e38\u88ab\u7528\u6765\u5fae\u8c03\u5176\u4ed6\u5e93\u51fd\u6570\u6240\u4f7f\u7528\u7684\u56de\u8c03\u51fd\u6570\u7684\u53c2\u6570\u3002\n\u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u6bb5\u4ee3\u7801\uff0c\u4f7f\u7528 multiprocessing \u6765\u5f02\u6b65\u8ba1\u7b97\u4e00\u4e2a\u7ed3\u679c\u503c\uff0c\n\u7136\u540e\u8fd9\u4e2a\u503c\u88ab\u4f20\u9012\u7ed9\u4e00\u4e2a\u63a5\u53d7\u4e00\u4e2aresult\u503c\u548c\u4e00\u4e2a\u53ef\u9009logging\u53c2\u6570\u7684\u56de\u8c03\u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def output_result(result, log=None):\n    if log is not None:\n        log.debug('Got: %r', result)\n\n# A sample function\ndef add(x, y):\n    return x + y\n\nif __name__ == '__main__':\n    import logging\n    from multiprocessing import Pool\n    from functools import partial\n\n    logging.basicConfig(level=logging.DEBUG)\n    log = logging.getLogger('test')\n\n    p = Pool()\n    p.apply_async(add, (3, 4), callback=partial(output_result, log=log))\n    p.close()\n    p.join()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u7ed9 apply_async() \u63d0\u4f9b\u56de\u8c03\u51fd\u6570\u65f6\uff0c\u901a\u8fc7\u4f7f\u7528 partial() \u4f20\u9012\u989d\u5916\u7684 logging \u53c2\u6570\u3002\n\u800c multiprocessing \u5bf9\u8fd9\u4e9b\u4e00\u65e0\u6240\u77e5\u2014\u2014\u5b83\u4ec5\u4ec5\u53ea\u662f\u4f7f\u7528\u5355\u4e2a\u503c\u6765\u8c03\u7528\u56de\u8c03\u51fd\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f5c\u4e3a\u4e00\u4e2a\u7c7b\u4f3c\u7684\u4f8b\u5b50\uff0c\u8003\u8651\u4e0b\u7f16\u5199\u7f51\u7edc\u670d\u52a1\u5668\u7684\u95ee\u9898\uff0csocketserver \u6a21\u5757\u8ba9\u5b83\u53d8\u5f97\u5f88\u5bb9\u6613\u3002\n\u4e0b\u9762\u662f\u4e2a\u7b80\u5355\u7684echo\u670d\u52a1\u5668\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from socketserver import StreamRequestHandler, TCPServer\n\nclass EchoHandler(StreamRequestHandler):\n    def handle(self):\n        for line in self.rfile:\n            self.wfile.write(b'GOT:' + line)\n\nserv = TCPServer(('', 15000), EchoHandler)\nserv.serve_forever()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0d\u8fc7\uff0c\u5047\u8bbe\u4f60\u60f3\u7ed9EchoHandler\u589e\u52a0\u4e00\u4e2a\u53ef\u4ee5\u63a5\u53d7\u5176\u4ed6\u914d\u7f6e\u9009\u9879\u7684 __init__ \u65b9\u6cd5\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class EchoHandler(StreamRequestHandler):\n    # ack is added keyword-only argument. *args, **kwargs are\n    # any normal parameters supplied (which are passed on)\n    def __init__(self, *args, ack, **kwargs):\n        self.ack = ack\n        super().__init__(*args, **kwargs)\n\n    def handle(self):\n        for line in self.rfile:\n            self.wfile.write(self.ack + line)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e48\u4fee\u6539\u540e\uff0c\u6211\u4eec\u5c31\u4e0d\u9700\u8981\u663e\u5f0f\u5730\u5728TCPServer\u7c7b\u4e2d\u6dfb\u52a0\u524d\u7f00\u4e86\u3002\n\u4f46\u662f\u4f60\u518d\u6b21\u8fd0\u884c\u7a0b\u5e8f\u540e\u4f1a\u62a5\u7c7b\u4f3c\u4e0b\u9762\u7684\u9519\u8bef\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "Exception happened during processing of request from ('127.0.0.1', 59834)\nTraceback (most recent call last):\n...\nTypeError: __init__() missing 1 required keyword-only argument: 'ack'"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u521d\u770b\u8d77\u6765\u597d\u50cf\u5f88\u96be\u4fee\u6b63\u8fd9\u4e2a\u9519\u8bef\uff0c\u9664\u4e86\u4fee\u6539 socketserver \u6a21\u5757\u6e90\u4ee3\u7801\u6216\u8005\u4f7f\u7528\u67d0\u4e9b\u5947\u602a\u7684\u65b9\u6cd5\u4e4b\u5916\u3002\n\u4f46\u662f\uff0c\u5982\u679c\u4f7f\u7528 partial() \u5c31\u80fd\u5f88\u8f7b\u677e\u7684\u89e3\u51b3\u2014\u2014\u7ed9\u5b83\u4f20\u9012 ack \u53c2\u6570\u7684\u503c\u6765\u521d\u59cb\u5316\u5373\u53ef\uff0c\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from functools import partial\nserv = TCPServer(('', 15000), partial(EchoHandler, ack=b'RECEIVED:'))\nserv.serve_forever()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c__init__() \u65b9\u6cd5\u4e2d\u7684ack\u53c2\u6570\u58f0\u660e\u65b9\u5f0f\u770b\u4e0a\u53bb\u5f88\u6709\u8da3\uff0c\u5176\u5b9e\u5c31\u662f\u58f0\u660eack\u4e3a\u4e00\u4e2a\u5f3a\u5236\u5173\u952e\u5b57\u53c2\u6570\u3002\n\u5173\u4e8e\u5f3a\u5236\u5173\u952e\u5b57\u53c2\u6570\u95ee\u9898\u6211\u4eec\u57287.2\u5c0f\u8282\u6211\u4eec\u5df2\u7ecf\u8ba8\u8bba\u8fc7\u4e86\uff0c\u8bfb\u8005\u53ef\u4ee5\u518d\u53bb\u56de\u987e\u4e00\u4e0b\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f88\u591a\u65f6\u5019 partial() \u80fd\u5b9e\u73b0\u7684\u6548\u679c\uff0clambda\u8868\u8fbe\u5f0f\u4e5f\u80fd\u5b9e\u73b0\u3002\u6bd4\u5982\uff0c\u4e4b\u524d\u7684\u51e0\u4e2a\u4f8b\u5b50\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u8fd9\u6837\u7684\u8868\u8fbe\u5f0f\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "points.sort(key=lambda p: distance(pt, p))\np.apply_async(add, (3, 4), callback=lambda result: output_result(result,log))\nserv = TCPServer(('', 15000),\n        lambda *args, **kwargs: EchoHandler(*args, ack=b'RECEIVED:', **kwargs))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u6837\u5199\u4e5f\u80fd\u5b9e\u73b0\u540c\u6837\u7684\u6548\u679c\uff0c\u4e0d\u8fc7\u76f8\u6bd4\u800c\u5df2\u4f1a\u663e\u5f97\u6bd4\u8f83\u81c3\u80bf\uff0c\u5bf9\u4e8e\u9605\u8bfb\u4ee3\u7801\u7684\u4eba\u6765\u8bb2\u4e5f\u66f4\u52a0\u96be\u61c2\u3002\n\u8fd9\u65f6\u5019\u4f7f\u7528 partial() \u53ef\u4ee5\u66f4\u52a0\u76f4\u89c2\u7684\u8868\u8fbe\u4f60\u7684\u610f\u56fe(\u7ed9\u67d0\u4e9b\u53c2\u6570\u9884\u5148\u8d4b\u503c)\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}